//
//  YZPersonViewController.m
//  个人详情控制器
//
//  Created by yz on 15/8/13.
//  Copyright (c) 2015年 yz. All rights reserved.
//

#import "FWPersonViewController.h"

#import "FWPersonTableViewController.h"
#import "FWTabBarView.h"

@interface FWPersonViewController ()<TabBarViewDelegate>

@property (nonatomic, weak) UILabel *titleLabel;
@property (weak, nonatomic) UIImage *navBackImage;

@property (weak, nonatomic) IBOutlet FWTabBarView *tabBar;

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headViewCons;

// 个人头像控件
@property (weak, nonatomic) IBOutlet UIImageView *personIconView;

// 个人明信片控件
@property (weak, nonatomic) IBOutlet UIImageView *personCardView;


@end

@implementation FWPersonViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:@"FWPersonViewController" bundle:nibBundleOrNil]) {
        
    }
    return self;
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 不自动添加额外滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _navBackImage = self.navigationController.navigationBar.backIndicatorImage;
    _tabBar.delegate = self;
    _personCardView.clipsToBounds = YES;
    _personCardView.layer.cornerRadius = _personCardView.frame.size.width * 0.5;
}

#pragma mark 设置导航条
- (void)setUpNav{
    // 导航条背景透明
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    // 设置导航条中间view
    UILabel *label = [[UILabel alloc] init];
    
    _titleLabel = label;
    
    label.font = [UIFont  boldSystemFontOfSize:18];
    
    label.text = self.title;
    
    [label setTextColor:[UIColor colorWithWhite:1 alpha:0]];
    
    [label sizeToFit];
    
    self.navigationItem.titleView = label;
}

#pragma mark 设置子控制器
- (void)setUpChildControlller{
    for (FWPersonTableViewController *personChildVc in self.childViewControllers) {
        
//        self.personIconView.image = self.personIconImage;
//        
//        self.personCardView.image = self.personCardImage;
        
        // 传递高度约束，用来移动头部视图
        personChildVc.headHCons = _headViewCons;
        
        // 传递标题控件，设置文字透明
        personChildVc.titleLabel = _titleLabel;
        
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setUpNav];
    
    [self onClickTabBar:nil from:0 to:0];
    
    // 设置子控制器
    [self setUpChildControlller];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:_navBackImage forBarMetrics:UIBarMetricsDefault];
}

- (void)onClickTabBar:(FWTabBarView *)tabBarView from:(NSInteger)from to:(NSInteger)to{
    // 将上一次选中的控制器的view移除内容视图
    UITableView *lastVcView = (UITableView *)[self.childViewControllers[from] view];
    
    [lastVcView removeFromSuperview];
    
    // 切换内容视图显示
    UITableViewController *vc = self.childViewControllers[to];
    
    vc.view.frame = _contentView.bounds;
    
    [_contentView addSubview:vc.view];
    
    // 设置tableView的滚动区域
    vc.tableView.contentOffset = lastVcView.contentOffset;
}


@end
